﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Proveedores.Servidor1;
using System.Net.Mail;
using System.Net;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        Service1 servidor;
        bool ResultadoAutenticacion;
        


        protected void Page_Load(object sender, EventArgs e)
        {
            servidor = new Service1();
            Session["Server"] = servidor; 
            if (!Page.IsPostBack)
            {
                if (Session["Server"] != null)
                {
                    List<string> listOfNames = servidor.Consultar(1).ToList();
                    Empresa.DataSource = listOfNames;
                    Empresa.DataBind();
                }                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            String empresa = Empresa.SelectedItem.Text;
            if ((TextBox1.Text != "") )
            {
                ResultadoAutenticacion = servidor.Autenticar_Proveedor(TextBox1.Text, TextBox2.Text, empresa);
            }

            if (ResultadoAutenticacion == true)
            {
                Session.Add("NombreUsuario", TextBox1.Text);
                Session.Add("NombreEmpresaUsuario", empresa);
                Label1.Text = "Sesion iniciada";
                Label1.Visible = true;
                TextBox1.Visible = false;
                Empresa.Visible = false;
                Label2.Visible = false;
                Label3.Visible = false;
                Button1.Visible = false;
                Label4.Visible = false;
                TextBox2.Visible = false;
            }
            else
            {
                Label1.Text = "Datos incorrectos. Por favor, verifíquelos.";
                Label1.Visible = true;
            }

            /*
            var fromAddress = new MailAddress("siaerp2012@gmail.com", "SIA-ERP");
            var toAddress = new MailAddress("", "");
            const string fromPassword = "siaerpii2012";
            const string subject = "Orden de Compra";
            const string body = "";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
             */
        }

        
    }
}